package com.itheima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.Category;
import com.itheima.common.PageParam;
import com.itheima.common.R;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: LiuBanXian
 * @Date: 2022/9/4  14:33
 * @description: 分类管理
 */
@RequestMapping("/category")
@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    /**
     * 分类管理::添加分类 | 套餐
     * @param category
     * @return
     */
    @PostMapping
    public R add(@RequestBody Category category ) {

        int row = categoryService.add(category);

        if (row > 0) {
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }

    /**
     * 分类管理::分页
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R findPage(PageParam pageParam) {
        IPage<Category> page = categoryService.findPage(pageParam);

        return R.success(page);
    }

    /**
     * 分类管理删除
     * @param id
     * @return
     */
    @DeleteMapping
    public R delete(long id) {
        int row = categoryService.delete(id);
        if (row > 0) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    /**
     * 分类管理修改信息
     * @param category
     * @return
     */
    @PutMapping
    public R update(@RequestBody Category category) {
        int row = categoryService.update(category);
        if (row > 0) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    /**
     * @description: 根据类型查询分类数据
     * @author: Zheng
     * @date: 2022/9/4 19:58
     * @param: [type]
     * @return: com.itheima.common.R
     **/
    @GetMapping("/list")
    public R findById(Integer type){

        List<Category> list = categoryService.findByType(type);
        return R.success(list);
    }

}
